#include "Vec2.h"

void Vec2::normalize()
{
    if (!this->isZero())
    {
        float lengthInvert = 1.f / std::sqrtf(x * x + y * y);
        this->x *= lengthInvert;
        this->y *= lengthInvert;
    }
}

Vec2 Vec2::getNormalized() const
{
    if (this->isZero())
    {
        return {0.f, 0.f};
    }
    else
    {
        float lengthInvert = 1.f / std::sqrtf(x * x + y * y);
        return {x * lengthInvert, y * lengthInvert};
    }
}

void Vec2::rotate(float angle)
{
    float sinAngle = std::sinf(angle);
    float cosAngle = std::cosf(angle);

    this->x = x * cosAngle - y * sinAngle;
    this->y = y * cosAngle + x * sinAngle;
}

float Vec2::angle(const Vec2 &v) const
{
    float dz = x * v.y - y * v.x;
    return atan2f(std::fabsf(dz), this->dot(v));
}

float Vec2::distance(const Vec2 &v) const
{
    float dx = x - v.x;
    float dy = y - v.y;
    return std::sqrtf(dx * dx + dy * dy);
}

float Vec2::distanceSquared(const Vec2 &v) const
{
    float dx = x - v.x;
    float dy = y - v.y;
    return dx * dx + dy * dy;
}

float Vec2::angle(const Vec2 &v1, const Vec2 &v2)
{
    return v1.angle(v2);
}
